package io.jpress.addon.interact.controller;

import com.jfinal.aop.Inject;
import com.jfinal.kit.Ret;
import io.jboot.utils.StrUtil;
import io.jboot.web.controller.annotation.RequestMapping;
import io.jpress.addon.interact.model.JpressAddonInteract;
import io.jpress.addon.interact.model.JpressAddonInteractComment;
import io.jpress.addon.interact.service.JpressAddonInteractCommentService;
import io.jpress.addon.interact.service.JpressAddonInteractService;
import io.jpress.model.User;
import io.jpress.service.UserService;
import io.jpress.web.base.TemplateControllerBase;

import java.util.Date;


@RequestMapping(value = "/interactSubmission")
public class SubmissionController extends TemplateControllerBase {

    @Inject
    private UserService userService;
    @Inject
    private JpressAddonInteractService interactService;
    @Inject
    private JpressAddonInteractCommentService commentService;

    /**
     * 提交问题
     */
    public void doSaveInteract() {

        User loginedUser = getLoginedUser();
        if(loginedUser==null){
            renderJson(Ret.fail().set("msg_ch","对不起您未登陆"));
            return;
        }
        String name = getPara("name");
        String phonenumber = getPara("phonenumber");
        String email = getPara("email");
        String title = getPara("title");
        String content = getPara("content");
        String attachment = getPara("attachment");

        if(name==null||name.trim().equals("")){
            renderJson(Ret.fail().set("msg_ch","姓名不能是空"));
            return;
        }
        if(phonenumber==null||phonenumber.trim().equals("")){
            renderJson(Ret.fail().set("msg_ch","电话不能是空"));
            return;
        }
        if(email==null||email.trim().equals("")){
            renderJson(Ret.fail().set("msg_ch","邮箱不能是空"));
            return;
        }
        if(title==null||title.trim().equals("")){
            renderJson(Ret.fail().set("msg_ch","标题不能是空"));
            return;
        }
        if(content==null||content.trim().equals("")){
            renderJson(Ret.fail().set("msg_ch","内容不能是空"));
            return;
        }

        name  = StrUtil.escapeHtml(name);
        phonenumber = StrUtil.escapeHtml(phonenumber);
        email = StrUtil.escapeHtml(email);
        title = StrUtil.escapeHtml(title);
        content = StrUtil.escapeHtml(content);
        attachment = StrUtil.escapeHtml(attachment);

        JpressAddonInteract jpressAddonInteract = new JpressAddonInteract();
        jpressAddonInteract.setUserId(loginedUser.getId().intValue());
        jpressAddonInteract.setName(name);
        jpressAddonInteract.setPhoneNumber(phonenumber);
        jpressAddonInteract.setEmail(email);
        jpressAddonInteract.setTitle(title);
        jpressAddonInteract.setContent(content);
        jpressAddonInteract.setAttachment(attachment);
        jpressAddonInteract.setCreateTime(new Date());
        interactService.save(jpressAddonInteract);
        renderJson(Ret.ok().set("msg_ch","提交成功"));
    }

    /**
     * 提交解答
     */
    public void doSaveInteractComment() {

        User loginedUser = getLoginedUser();
        if(loginedUser==null){
            renderJson(Ret.fail().set("msg_ch","对不起您未登陆"));
            return;
        }
        Integer interactid = getParaToInt("interactid");
        String name = getPara("name");
        String phonenumber = getPara("phonenumber");
        String email = getPara("email");
        String content = getPara("content");
        String attachment = getPara("attachment");
        if(interactid==null){
            renderJson(Ret.fail().set("msg_ch","interactid不能是空"));
            return;
        }
        JpressAddonInteract jpressAddonInteract = interactService.findById(interactid);
        if(jpressAddonInteract==null){
            renderJson(Ret.fail().set("msg_ch","此条帖子不存在"));
            return;
        }
        if(name==null||name.trim().equals("")){
            renderJson(Ret.fail().set("msg_ch","姓名不能是空"));
            return;
        }
        if(phonenumber==null||phonenumber.trim().equals("")){
            renderJson(Ret.fail().set("msg_ch","电话不能是空"));
            return;
        }
        if(email==null||email.trim().equals("")){
            renderJson(Ret.fail().set("msg_ch","邮箱不能是空"));
            return;
        }
        if(content==null||content.trim().equals("")){
            renderJson(Ret.fail().set("msg_ch","内容不能是空"));
            return;
        }

        name  = StrUtil.escapeHtml(name);
        phonenumber = StrUtil.escapeHtml(phonenumber);
        email = StrUtil.escapeHtml(email);
        content = StrUtil.escapeHtml(content);
        attachment = StrUtil.escapeHtml(attachment);

        JpressAddonInteractComment jpressAddonInteractComment = new JpressAddonInteractComment();
        jpressAddonInteractComment.setUserId(loginedUser.getId().intValue());
        jpressAddonInteractComment.setInteractId(interactid);
        jpressAddonInteractComment.setName(name);
        jpressAddonInteractComment.setPhoneNumber(phonenumber);
        jpressAddonInteractComment.setEmail(email);
        jpressAddonInteractComment.setContent(content);
        jpressAddonInteractComment.setAttachment(attachment);
        jpressAddonInteractComment.setCreateTime(new Date());
        commentService.save(jpressAddonInteractComment);


        JpressAddonInteract byId = interactService.findById(interactid);
        byId.setCreateTime(new Date());
        interactService.update(byId);

        renderJson(Ret.ok().set("msg_ch","提交成功"));
    }

}
